import http from '../../apis/api.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    imageList:Array
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**点击添加图片*/ 
    chooseImageTap(){
      let self = this;
      wx.showActionSheet({
        itemList: ['从相册中选择', '拍照'],
        itemColor:'#270c0b',
        success(res) {
          switch (res.tapIndex){
            case 0:
              self.chooseWxImage('album')
            break;
            case 1:
              self.chooseWxImage('camera')
            break;
          }
        },
        fail(res) {
          console.log(res.errMsg)
        }
      })
    },
    /**选择图片*/ 
    chooseWxImage(type){
      let self = this;
      wx.chooseImage({
        count:3,
        sizeType: ['original', 'compressed'],
        sourceType: [type],
        success(res){
          if (res.tempFilePaths.length>0){
            res.tempFilePaths.forEach(item=>{
              self.uploadImage(item);
            })
          }
        }
      })
    },
    /**图片上传服务器*/ 
    uploadImage(imageUrl){
      let self = this;
      http.imageUpload(imageUrl).then(res=>{
        let result = JSON.parse(res)
        self.data.imageList.push({ src: imageUrl, path: result.content[0]});
        self.setData({
          imageList: self.data.imageList
        })
      },error=>{
        console.log(error);
      })
    },
    /**删除图片*/ 
    removeImage(e){
      let self = this;
      http.deleteImage({
        path: self.data.imageList[e.currentTarget.dataset.index].path
      }).then(res=>{
        self.data.imageList.splice(e.currentTarget.dataset.index,1);
        self.setData({
          imageList: self.data.imageList
        })
      })
    },
    /**获取图片列表*/ 
    getImageList(){

    }
  }
})
